//
//  RegisterVC.m
//  Book
//
//  Created by zhiyou on 16-3-22.
//  Copyright (c) 2016年 陈凯. All rights reserved.
//

#import "RegisterVC.h"
#import "Masonry.h"
#import "MyTable.h"
#import "Users.h"
#import "Custom.h"
#define  UserDefault [NSUserDefaults standardUserDefaults]
@interface RegisterVC ()<UIAlertViewDelegate>

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.title=@"新用户注册";
    UIImageView*bgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"背景"]];
    bgView.frame=[UIScreen mainScreen].bounds;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.offset(0);
    }];
    //新用户名
    _NameTF=[[UITextField alloc]init];
    _NameTF.placeholder=@"请输入用户名";
    _NameTF.textAlignment=NSTextAlignmentCenter ;
    _NameTF.borderStyle=UITextBorderStyleRoundedRect;
    UIImageView*imgV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"手机"]];
    [self.view addSubview:_NameTF];
    _NameTF.leftView=imgV;
    _NameTF.leftViewMode= UITextFieldViewModeAlways;
    [_NameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-30);
    }];
    _Namelabel=[[UILabel alloc]init];
    _Namelabel.text=@"用户名：";
    [self.view addSubview:_Namelabel];
    [_Namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(70);
        make.centerY.equalTo(_NameTF.mas_centerY);
        make.right.equalTo(_NameTF.mas_left).offset(5);
        make.left.offset(15);
        
    }];
    //填写密码
    _PwdTF=[[UITextField alloc]init];
    _PwdTF.placeholder=@"请输入密码";
    _PwdTF.textAlignment=NSTextAlignmentCenter ;
    _PwdTF.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:_PwdTF];
    UIImageView*imgV1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"密码"]];
    _PwdTF.leftView=imgV1;
    _PwdTF.leftViewMode= UITextFieldViewModeAlways;
    [_PwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-30);
    }];
    _pwdlabel=[[UILabel alloc]init];
    _pwdlabel.text=@"密码：";
    [self.view addSubview:_pwdlabel];
    [_pwdlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_Namelabel.mas_bottom).offset(20);
        make.right.equalTo(_PwdTF.mas_left).offset(5);
        make.centerY.equalTo(_PwdTF.mas_centerY);
        
        make.left.offset(28);
        
    }];
    //确认密码
    _Pwd1TF=[[UITextField alloc]init];
    _Pwd1TF.placeholder=@"请再输入密码";
    _Pwd1TF.textAlignment=NSTextAlignmentCenter ;
    _Pwd1TF.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:_Pwd1TF];
    UIImageView*imgV2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"密码"]];
    _Pwd1TF.leftView=imgV2;
    _Pwd1TF.leftViewMode= UITextFieldViewModeAlways;
    [_Pwd1TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-30);
    }];
    _pwdlabel1=[[UILabel alloc]init];
    _pwdlabel1.text=@"确认密码：";
    [self.view addSubview:_pwdlabel1];
    [_pwdlabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_pwdlabel.mas_bottom).offset(20);
        make.right.equalTo(_Pwd1TF.mas_left).offset(5);
        make.centerY.equalTo(_Pwd1TF.mas_centerY);
        
        make.left.offset(10);
        
    }];
    //邮箱
   _messageTF=[[UITextField alloc]init];
    _messageTF.placeholder=@"请输入邮箱";
    _messageTF.textAlignment=NSTextAlignmentCenter ;
    _messageTF.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:_messageTF];
    [_messageTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-30);
    }];
    _messagelabel=[[UILabel alloc]init];
    _messagelabel.text=@"邮箱：";
    [self.view addSubview:_messagelabel];
    [_messagelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_pwdlabel1.mas_bottom).offset(20);
        make.right.equalTo(_messageTF.mas_left).offset(5);
        make.centerY.equalTo(_messageTF.mas_centerY);
        
        make.left.offset(10);
        
    }];
    
    //昵称
    
    _nickTF=[[UITextField alloc]init];
    _nickTF.placeholder=@"请输入昵称";
    _nickTF.textAlignment=NSTextAlignmentCenter ;
    _nickTF.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:_nickTF];
    [_nickTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-30);
    }];
    _nicklabel=[[UILabel alloc]init];
    _nicklabel.text=@"昵称：";
    [self.view addSubview:_nicklabel];
    [_nicklabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_messagelabel.mas_bottom).offset(20);
        make.right.equalTo(_nickTF.mas_left).offset(5);
        make.centerY.equalTo(_nickTF.mas_centerY);
        
        make.left.offset(10);
        
    }];

    
    //注册
        _RegisterButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_RegisterButton setTitle:@"注册" forState:UIControlStateNormal];
   
    [_RegisterButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [_RegisterButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:_RegisterButton];
    [_RegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
    }];
    [_RegisterButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];
}
-(void)click{
     if([_Pwd1TF.text isEqualToString:_PwdTF.text]&&_PwdTF.text.length!=0&&_NameTF.text.length!=0){
         [Custom registerOrLoginRequestWithCommand:@"2" Name:_NameTF.text Psw:_PwdTF.text Nickname:_nickTF.text Email:_messageTF.text Comletion:^(NSString *result) {
             [Custom setMessage:result];
             if([result isEqualToString:@"注册成功"]){
                 [self.navigationController popViewControllerAnimated:YES];
             }else{
                 return ;
             }
         }];
     }
//    [Custom RegisterWithName:_NameTF.text pwd:_PwdTF.text Nick:_nickTF.text email:_messageTF.text command:1 commpletion:^(NSString *result) {
//        if([result isEqualToString:@"注册成功"]){
//            [Custom setMessage:result];
//            [UIView setAnimationDelay:3];
//            [self.navigationController popViewControllerAnimated:YES];
//        }else{
//            [Custom setMessage:result];
//        }
//    }];
//     }else{
//         [Custom setMessage:@"用户名或密码不正确,请重输"];
//         _NameTF.text=nil;
//         _PwdTF.text=nil;
//         _nickTF.text=nil;
//         _Pwd1TF.text=nil;
//         _messageTF.text=nil;
//     }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
